package com.cjc.myimageload.lib;

import java.io.File;

import android.content.Context;

import com.cjc.myimageload.tools.Constants;

public class FileCache {

	private File cacheDir;

	public FileCache(Context context) {
		// 找一个用来缓存图片的路径
		if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
			cacheDir = new File(Constants.IMAGEURI);
		else {
			cacheDir = context.getCacheDir();
		}
		if (!cacheDir.exists()) {
			cacheDir.mkdirs();
		}
	}

	public File getFile(String url, String time) {
		System.out.println("url:" + url);
		String filename = String.valueOf(Encrypt.md5(url + time));
		// String filename = url.substring(url.lastIndexOf("/"));
		File f = new File(cacheDir, filename);
		System.out.println("文件路径:" + f.getPath());
		return f;

	}

	public void clear() {
		File[] files = cacheDir.listFiles();
		if (files == null)
			return;
		for (File f : files)
			f.delete();
	}

}
